home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / program / 318 / utilsrc / strip.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-10-20  |  19.0 KB  |  666 lines

  1. /* strip certain symbols from a rel file.
  2.    Copyright (C) 1986 Free Software Foundation, Inc.
  3.  
  4.                NO WARRANTY
  5.  
  6.   BECAUSE THIS PROGRAM IS LICENSED FREE OF CHARGE, WE PROVIDE ABSOLUTELY
  7. NO WARRANTY, TO THE EXTENT PERMITTED BY APPLICABLE STATE LAW.  EXCEPT
  8. WHEN OTHERWISE STATED IN WRITING, FREE SOFTWARE FOUNDATION, INC,
  9. RICHARD M. STALLMAN AND/OR OTHER PARTIES PROVIDE THIS PROGRAM "AS IS"
  10. WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
  11. BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS TO THE QUALITY
  13. AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE PROGRAM PROVE
  14. DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR
  15. CORRECTION.
  16.  
  17.  IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW WILL RICHARD M.
  18. STALLMAN, THE FREE SOFTWARE FOUNDATION, INC., AND/OR ANY OTHER PARTY
  19. WHO MAY MODIFY AND REDISTRIBUTE THIS PROGRAM AS PERMITTED BELOW, BE
  20. LIABLE TO YOU FOR DAMAGES, INCLUDING ANY LOST PROFITS, LOST MONIES, OR
  21. OTHER SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
  22. USE OR INABILITY TO USE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR
  23. DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY THIRD PARTIES OR
  24. A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS) THIS
  25. PROGRAM, EVEN IF YOU HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH
  26. DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY.
  27.  
  28.         GENERAL PUBLIC LICENSE TO COPY
  29.  
  30.   1. You may copy and distribute verbatim copies of this source file
  31. as you receive it, in any medium, provided that you conspicuously
  32. and appropriately publish on each copy a valid copyright notice
  33. "Copyright (C) 1986 Free Software Foundation, Inc.", and include
  34. following the copyright notice a verbatim copy of the above disclaimer
  35. of warranty and of this License.
  36.  
  37.   2. You may modify your copy or copies of this source file or
  38. any portion of it, and copy and distribute such modifications under
  39. the terms of Paragraph 1 above, provided that you also do the following:
  40.  
  41.     a) cause the modified files to carry prominent notices stating
  42.     that you changed the files and the date of any change; and
  43.  
  44.     b) cause the whole of any work that you distribute or publish,
  45.     that in whole or in part contains or is a derivative of this
  46.     program or any part thereof, to be licensed at no charge to all
  47.     third parties on terms identical to those contained in this
  48.     License Agreement (except that you may choose to grant more extensive
  49.     warranty protection to some or all third parties, at your option).
  50.  
  51.     c) You may charge a distribution fee for the physical act of
  52.     transferring a copy, and you may at your option offer warranty
  53.     protection in exchange for a fee.
  54.  
  55. Mere aggregation of another unrelated program with this program (or its
  56. derivative) on a volume of a storage or distribution medium does not bring
  57. the other program under the scope of these terms.
  58.  
  59.   3. You may copy and distribute this program (or a portion or derivative
  60. of it, under Paragraph 2) in object code or executable form under the terms
  61. of Paragraphs 1 and 2 above provided that you also do one of the following:
  62.  
  63.     a) accompany it with the complete corresponding machine-readable
  64.     source code, which must be distributed under the terms of
  65.     Paragraphs 1 and 2 above; or,
  66.  
  67.     b) accompany it with a written offer, valid for at least three
  68.     years, to give any third party free (except for a nominal
  69.     shipping charge) a complete machine-readable copy of the
  70.     corresponding source code, to be distributed under the terms of
  71.     Paragraphs 1 and 2 above; or,
  72.  
  73.     c) accompany it with the information you received as to where the
  74.     corresponding source code may be obtained.  (This alternative is
  75.     allowed only for noncommercial distribution and only if you
  76.     received the program in object code or executable form alone.)
  77.  
  78. For an executable file, complete source code means all the source code for
  79. all modules it contains; but, as a special exception, it need not include
  80. source code for modules which are standard libraries that accompany the
  81. operating system on which the executable file runs.
  82.  
  83.   4. You may not copy, sublicense, distribute or transfer this program
  84. except as expressly provided under this License Agreement.  Any attempt
  85. otherwise to copy, sublicense, distribute or transfer this program is void and
  86. your rights to use the program under this License agreement shall be
  87. automatically terminated.  However, parties who have received computer
  88. software programs from you with this License Agreement will not have
  89. their licenses terminated so long as such parties remain in full compliance.
  90.  
  91.   5. If you wish to incorporate parts of this program into other free
  92. programs whose distribution conditions are different, write to the Free
  93. Software Foundation at 675 Mass Ave, Cambridge, MA 02139.  We have not yet
  94. worked out a simple rule that can be stated here, but we will often permit
  95. this.  We will be guided by the two goals of preserving the free status of
  96. all derivatives of our free software and of promoting the sharing and reuse of
  97. software.
  98.  
  99.  In other words, you are welcome to use, share and improve this program.
  100.  You are forbidden to forbid anyone else to use, share and improve
  101.  what you give them.   Help stamp out software-hoarding!  */
  102.  
  103. #include <a.out.h>
  104. #include <stdio.h>
  105. #include <strings.h>
  106. #include <sys/file.h>
  107.  
  108. /* Count the number of nlist entries that are for local symbols. */
  109. int local_sym_count;
  110.  
  111. /* Count number of nlist entries that are for local symbols
  112.    whose names don't start with L. */
  113. int non_L_local_sym_count;
  114.  
  115. /* Count the number of nlist entries for debugger info.  */
  116. int debugger_sym_count;
  117.  
  118. /* Count the number of global symbols referenced or defined.  */
  119. int global_sym_count;
  120.  
  121. /* Total number of symbols to be preserved in the current file.  */
  122. int nsyms;
  123.  
  124. /* Number of files specified in the command line. */
  125.  
  126. int number_of_files;
  127.  
  128. /* Each specified file has a file_entry structure for it.
  129.    These are contained in the vector which file_table points to.  */
  130.  
  131. struct file_entry {
  132.   char *filename;
  133.   struct exec header;        /* the file's header */
  134.   int ss_size;            /* size, in bytes, of symbols_and_strings data */
  135.   struct nlist *symbols_and_strings;
  136. };
  137.  
  138. struct file_entry *file_table;
  139.  
  140. /* Descriptor on which current file is open.  */
  141.  
  142. int input_desc;
  143.  
  144. /* Stream for writing that file using stdio.  */
  145.  
  146. FILE *outstream;
  147.  
  148. /* 1 => strip all symbols; 2 => strip all debugger symbols */
  149. int strip_symbols;
  150.  
  151. /* 1 => discard locals starting with L; 2 => discard all locals */
  152. int discard_locals;
  153.  
  154. void strip_file ();
  155. int file_open ();
  156. void read_file_symbols (), rewrite_file_symbols(), file_close();
  157. void read_header(), read_file_symbols (), read_entry_symbols();
  158. void count_file_symbols ();
  159. char *concat ();
  160.  
  161. main (argc, argv)
  162.      char **argv;
  163.      int argc;
  164. {
  165.   int c;
  166.   extern int optind;
  167.   struct file_entry *p;
  168.   int i;
  169.  
  170.   strip_symbols = 0;   /* default is to strip everything.  */
  171.   discard_locals = 0;
  172.  
  173.   while ((c = getopt (argc, argv, "sSxX")) != EOF)
  174.     switch (c)
  175.       {
  176.       case 's':
  177.     strip_symbols = 1;
  178.     break;
  179.       case 'S':
  180.     strip_symbols = 2;
  181.     break;
  182.       case 'x':
  183.     discard_locals = 2;
  184.     break;
  185.       case 'X':
  186.     discard_locals = 1;
  187.     break;
  188.       }
  189.  
  190.   /* Default is to strip all symbols.  */
  191.   if (strip_symbols == 0 && discard_locals == 0)
  192.     strip_symbols = 1;
  193.  
  194.   number_of_files = argc - optind;
  195.  
  196.   if (!number_of_files)
  197.     fatal ("no files specified", 0);
  198.  
  199.   p = file_table
  200.     = (struct file_entry *) xmalloc (number_of_files * sizeof (struct file_entry));
  201.  
  202.   /* Now fill in file_table */
  203.  
  204.   for (i = 0; i < number_of_files; i++)
  205.     {
  206.       p->filename = argv[i + optind];
  207.       p->symbols_and_strings = 0;
  208.       p++;
  209.     }
  210.  
  211.   for (i = 0; i < number_of_files; i++)
  212.     strip_file (&file_table[i]);
  213. }
  214.  
  215. /* process one input file */
  216.  
  217. void
  218. strip_file (entry)
  219.      struct file_entry *entry;
  220. {
  221.   local_sym_count = 0;
  222.   non_L_local_sym_count = 0;
  223.   debugger_sym_count = 0;
  224.   global_sym_count = 0;
  225.  
  226.   file_open (entry);
  227.   if (strip_symbols != 1)
  228.     /* read in the existing symbols unless we are discarding everything.  */
  229.     read_file_symbols (entry);
  230.   rewrite_file_symbols (entry);
  231.   if (strip_symbols != 1)
  232.     free (entry->symbols_and_strings);
  233.   file_close ();
  234. }
  235.  
  236. /** Convenient functions for operating on one or all files being processed.  */
  237.  
  238. /* Close the file that is now open.  */
  239.  
  240. void
  241. file_close ()
  242. {
  243.   close (input_desc);
  244.   input_desc = 0;
  245. }
  246.  
  247. /* Open the file specified by 'entry', and return a descriptor.
  248.    The descriptor is also saved in input_desc.  */
  249.  
  250. /* JF this also makes sure the file is in rel format */
  251.  
  252. int
  253. file_open (entry)
  254.      struct file_entry *entry;
  255. {
  256.   int desc;
  257.   int len, magicnum;
  258.  
  259.   desc = open (entry->filename, O_RDWR, 0);
  260.  
  261.   if (desc > 0)
  262.     {
  263.       input_desc = desc;
  264.       len = read (input_desc, (char *)&magicnum, sizeof magicnum);
  265.       if (len != sizeof magicnum)
  266.     fatal_with_file ("failure reading header of ", entry);
  267.       if (N_BADMAG (*((struct exec *)&magicnum)))
  268.     fatal_with_file ("malformed input file (not a rel file) ", entry);
  269.       read_header (desc, &entry->header, entry);
  270.       return desc;
  271.     }
  272.  
  273.   perror_file (entry);
  274. }
  275.  
  276. /* Print the filename of ENTRY on OUTFILE (a stdio stream), then a newline.  */
  277.  
  278. prline_file_name (entry, outfile)
  279.      struct file_entry *entry;
  280.      FILE *outfile;
  281. {
  282.   print_file_name (entry, outfile);
  283.   fprintf (outfile, "\n");
  284. }
  285.  
  286. /* Print the filename of ENTRY on OUTFILE (a stdio stream).  */
  287.  
  288. print_file_name (entry, outfile)
  289.      struct file_entry *entry;
  290.      FILE *outfile;
  291. {
  292.   fprintf (outfile, "%s", entry->filename);
  293. }
  294.  
  295. /* Validate file ENTRY and read its symbol and string sections into core. */
  296.  
  297. void
  298. read_file_symbols (entry)
  299.      struct file_entry *entry;
  300. {
  301.   read_entry_symbols (input_desc, entry);
  302.   count_file_symbols (entry);
  303. }
  304.  
  305. /* Read a file's header into the proper place in the file_entry.  */
  306.  
  307. void
  308. read_header (desc, loc, entry)
  309.      int desc;
  310.      struct exec *loc;
  311.      struct file_entry *entry;
  312. {
  313.   int len;
  314.   lseek (desc,0, 0);
  315.   len = read (desc, loc, sizeof (struct exec));
  316.   if (len != sizeof (struct exec))
  317.     fatal_with_file ("failure reading header of ", entry);
  318.   if (N_BADMAG (*loc))
  319.     fatal_with_file ("bad magic number in ", entry);
  320. }
  321.  
  322. /* Read the symbols and strings of file ENTRY into core.
  323.    Assume it is already open, on descriptor DESC.  */
  324.  
  325. void
  326. read_entry_symbols (desc, entry)
  327.      struct file_entry *entry;
  328.      int desc;
  329. {
  330.   int string_size;
  331.  
  332.   lseek (desc, N_STROFF (entry->header), 0);
  333.   if (sizeof string_size != read (desc, &string_size, sizeof string_size))
  334.     fatal_with_file ("bad string table in ", entry);
  335.  
  336.   entry->ss_size = string_size + entry->header.a_syms;
  337.   entry->symbols_and_strings = (struct nlist *) xmalloc (entry->ss_size);
  338.  
  339.   lseek (desc, N_SYMOFF (entry->header), 0);
  340.   if (entry->ss_size != read (desc, entry->symbols_and_strings, entry->ss_size))
  341.     fatal_with_file ("premature end of file in symbols/strings of ", entry);
  342. }
  343.  
  344.  
  345. /* Count the number of symbols of various categories in the file of ENTRY.  */
  346.  
  347. void
  348. count_file_symbols (entry)
  349.      struct file_entry *entry;
  350. {
  351.   struct nlist *p, *end = entry->symbols_and_strings + entry->header.a_syms / sizeof (struct nlist);
  352.   char *name_base = entry->header.a_syms + (char *) entry->symbols_and_strings;
  353.  
  354.   for (p = entry->symbols_and_strings; p < end; p++)
  355.     if (p->n_type & N_EXT)
  356.       global_sym_count++;
  357.     else if (p->n_un.n_strx && !(p->n_type & (N_STAB | N_EXT)))
  358.       {
  359.     if ((p->n_un.n_strx + name_base)[0] != 'L')
  360.       non_L_local_sym_count++;
  361.     local_sym_count++;
  362.       }
  363.     else debugger_sym_count++;
  364. }
  365.  
  366. void write_file_syms (), modify_relocation ();
  367.  
  368. /* Total size of string table strings allocated so far */
  369. int strtab_size;
  370.  
  371. /* Vector whose elements are the strings to go in the string table */
  372. char **strtab_vector;
  373.  
  374. /* Index in strtab_vector at which the next string will be stored */
  375. int strtab_index;
  376.  
  377. int sym_written_count;
  378.  
  379. int
  380. assign_string_table_index (name)
  381.      char *name;
  382. {
  383.   int index = strtab_size;
  384.  
  385.   strtab_size += strlen (name) + 1;
  386.   strtab_vector[strtab_index++] = name;
  387.  
  388.   return index;
  389. }
  390.  
  391. void
  392. rewrite_file_symbols (entry)
  393.      struct file_entry *entry;
  394. {
  395.   int i;
  396.   struct nlist *newsyms;
  397.  
  398.   /* Calculate number of symbols to be preserved.  */
  399.  
  400.   if (strip_symbols == 1)
  401.     nsyms = 0;
  402.   else
  403.     {
  404.       nsyms = global_sym_count;
  405.       if (discard_locals == 1)
  406.     nsyms += non_L_local_sym_count;
  407.       else if (discard_locals == 0)
  408.     nsyms += local_sym_count;
  409.     }
  410.  
  411.   if (strip_symbols == 0)
  412.     nsyms += debugger_sym_count;
  413.  
  414.   strtab_vector = (char **) xmalloc (nsyms * sizeof (char *));
  415.   strtab_index = 0;
  416.  
  417.   strtab_size = 4;
  418.  
  419.   /* Accumulate in 'newsyms' the symbol table to be written.  */
  420.  
  421.   newsyms = (struct nlist *) xmalloc (nsyms * sizeof (struct nlist));
  422.  
  423.   sym_written_count = 0;
  424.  
  425.   if (strip_symbols != 1)
  426.     /* Write into newsyms the symbols we want to keep.  */
  427.     write_file_syms (entry, newsyms);
  428.  
  429.   if (sym_written_count != nsyms)
  430.     {
  431.       fprintf (stderr, "written = %d, expected = %d\n",
  432.            sym_written_count, nsyms);
  433.       fatal_with_file ("internal error: wrong number of symbols written in ",
  434.                entry);
  435.     }
  436.  
  437.   /* Modify the symbol-numbers in the relocation in the file,
  438.      to preserve its meaning */
  439.   modify_relocation (input_desc, entry);
  440.  
  441.   /* Now write contents of NEWSYMS into the file. */
  442.  
  443.   lseek (input_desc, N_SYMOFF (entry->header), 0);
  444.   write (input_desc, newsyms, nsyms * sizeof (struct nlist));
  445.   free (newsyms);
  446.  
  447.   /* Now write the string table.  */
  448.  
  449.   {
  450.     char *strvec = (char *) xmalloc (strtab_size);
  451.     char *p;
  452.  
  453.     *((long *) strvec) = strtab_size;
  454.  
  455.     p = strvec + sizeof (long);
  456.  
  457.     for (i = 0; i < strtab_index; i++)
  458.       {
  459.     int len = strlen (strtab_vector[i]);
  460.     strcpy (p, strtab_vector[i]);
  461.     *(p+len) = 0;
  462.     p += len + 1;
  463.       }
  464.  
  465.     write (input_desc, strvec, strtab_size);
  466.     free (strvec);
  467.   }
  468.  
  469.   /* Adjust file to be smaller */
  470.  
  471.   if (ftruncate (input_desc, tell (input_desc)) < 0)
  472.     perror_file (entry);
  473.  
  474.   /* Write new symbol table size into file header.  */
  475.  
  476.   entry->header.a_syms = nsyms * sizeof (struct nlist);
  477.  
  478.   lseek (input_desc, 0, 0);
  479.   write (input_desc, &entry->header, sizeof (struct exec));
  480.  
  481.   free (strtab_vector);
  482. }
  483.  
  484. /* Copy into NEWSYMS the symbol entries to be preserved.
  485.    Count them in sym_written_count.  */
  486.  
  487. /* We record, for each symbol written, its symbol number in the resulting file.
  488.    This is so that the relocation can be updated later.
  489.    Since the symbol names will not be needed again,
  490.    this index goes in the `n_strx' field.
  491.    If a symbol is not written, -1 is stored there.  */
  492.  
  493. void
  494. write_file_syms (entry, newsyms)
  495.      struct file_entry *entry;
  496.      struct nlist *newsyms;
  497. {
  498.   struct nlist *p = entry->symbols_and_strings;
  499.   struct nlist *end = p + entry->header.a_syms / sizeof (struct nlist);
  500.   char *string_base = (char *) end;   /* address of start of file's string table */
  501.   struct nlist *outp = newsyms;
  502.  
  503.   for (; p < end; p++)
  504.     {
  505.       int type = p->n_type;
  506.       int write;
  507.   
  508.       if (p->n_type & N_EXT)
  509.     write = 1;
  510.       else if (p->n_un.n_strx && !(p->n_type & (N_STAB | N_EXT)))
  511.     /* ordinary local symbol */
  512.     write = (discard_locals != 2)
  513.         && !(discard_locals == 1 &&
  514.              (p->n_un.n_strx + string_base)[0] == 'L');
  515.       else
  516.     /* debugger symbol */
  517.     write = (strip_symbols == 0);
  518.  
  519.       if (write)
  520.     {
  521.       if (p->n_un.n_strx)
  522.         p->n_un.n_strx = assign_string_table_index (p->n_un.n_strx + string_base);
  523.  
  524.       *outp++ = *p;
  525.  
  526.       p->n_un.n_strx = sym_written_count++;
  527.     }
  528.       else p->n_un.n_strx = -1;
  529.     }
  530. }
  531.  
  532. /* Read in ENTRY's relocation, alter the symbolnums in it,
  533.    and write it out again.  */
  534.  
  535. void
  536. modify_relocation (desc, entry)
  537.      int desc;
  538.      struct file_entry *entry;
  539. {
  540.   struct relocation_info *reloc, *p, *end;
  541.   int size = entry->header.a_trsize + entry->header.a_drsize;
  542.   struct nlist *sym_base = (struct nlist *) entry->symbols_and_strings;
  543.  
  544.   reloc = (struct relocation_info *) xmalloc (size);
  545.   lseek (desc, N_TXTOFF (entry->header) + entry->header.a_text + entry->header.a_data, 0);
  546.   read (desc, reloc, size);
  547.  
  548.   p = reloc;
  549.   end = (struct relocation_info *) (size + (char *) reloc);
  550.   while (p < end)
  551.     {
  552.       if (p->r_extern)
  553.     {
  554.       int newnum = (sym_base == 0 ? -1
  555.             :(sym_base + p->r_symbolnum) -> n_un.n_strx);
  556.       if (newnum < 0)
  557.         fatal_with_file ("stripped symbol needed for relocation in ", entry);
  558.       p->r_symbolnum = newnum;
  559.     }
  560.       p++;
  561.     }
  562.  
  563.   lseek (desc, N_TXTOFF (entry->header) + entry->header.a_text + entry->header.a_data, 0);
  564.   write (desc, reloc, size);
  565. }
  566.  
  567. /* Report a fatal error.
  568.    STRING is a printf format string and ARG is one arg for it.  */
  569.  
  570. fatal (string, arg)
  571.      char *string, *arg;
  572. {
  573.   fprintf (stderr, "strip: ");
  574.   fprintf (stderr, string, arg);
  575.   fprintf (stderr, "\n");
  576.   exit (1);
  577. }
  578.  
  579. /* Report a fatal error.
  580.    STRING is printed, followed by the filename of ENTRY.  */
  581.  
  582. fatal_with_file (string, entry)
  583.      char *string;
  584.      struct file_entry *entry;
  585. {
  586.   fprintf (stderr, "strip: ");
  587.   fprintf (stderr, string);
  588.   print_file_name (entry, stderr);
  589.   fprintf (stderr, "\n");
  590.   exit (1);
  591. }
  592.  
  593. /* Report a fatal error using the message for the last failed system call,
  594.    followed by the string NAME.  */
  595.  
  596. perror_name (name)
  597.      char *name;
  598. {
  599.   extern int errno, sys_nerr;
  600.   extern char *sys_errlist[];
  601.   char *s;
  602.  
  603.   if (errno < sys_nerr)
  604.     s = concat ("", sys_errlist[errno], " for %s");
  605.   else
  606.     s = "cannot open %s";
  607.   fatal (s, name);
  608. }
  609.  
  610. /* Report a fatal error using the message for the last failed system call,
  611.    followed by the name of file ENTRY.  */
  612.  
  613. perror_file (entry)
  614.      struct file_entry *entry;
  615. {
  616.   extern int errno, sys_nerr;
  617.   extern char *sys_errlist[];
  618.   char *s;
  619.  
  620.   if (errno < sys_nerr)
  621.     s = concat ("", sys_errlist[errno], " for ");
  622.   else
  623.     s = "cannot open ";
  624.   fatal_with_file (s, entry);
  625. }
  626.  
  627. /* Report a nonfatal error.
  628.    STRING is a format for printf, and ARG1 ... ARG3 are args for it.  */
  629.  
  630. error (string, arg1, arg2, arg3)
  631.      char *string, *arg1, *arg2, *arg3;
  632. {
  633.   fprintf (stderr, string, arg1, arg2, arg3);
  634.   fprintf (stderr, "\n");
  635. }
  636.  
  637. /* Return a newly-allocated string whose contents 
  638.    concatenate those of S1, S2, S3.  */
  639.  
  640. char *
  641. concat (s1, s2, s3)
  642.      char *s1, *s2, *s3;
  643. {
  644.   int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
  645.   char *result = (char *) xmalloc (len1 + len2 + len3 + 1);
  646.  
  647.   strcpy (result, s1);
  648.   strcpy (result + len1, s2);
  649.   strcpy (result + len1 + len2, s3);
  650.   *(result + len1 + len2 + len3) = 0;
  651.  
  652.   return result;
  653. }
  654.  
  655. /* Like malloc but get fatal error if memory is exhausted.  */
  656.  
  657. int
  658. xmalloc (size)
  659.      int size;
  660. {
  661.   int result = malloc (size);
  662.   if (!result)
  663.     fatal ("virtual memory exhausted", 0);
  664.   return result;
  665. }
  666.